package com.nineclock.system.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSS;
import com.nineclock.common.constant.SMSConstant;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.sms.AliyunSmsUtils;
import com.nineclock.common.utils.PhoneNumCheckUtils;
import com.nineclock.system.service.SMSService;
import com.nineclock.system.service.SysCompanyUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class SMSServiceImpl implements SMSService {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    OSS oss;
    @Autowired
    AliyunSmsUtils smsUtils;
    @Autowired
    SysCompanyUserService sysCompanyUserService;

    @Override
    public void sendCode(String mobile, String type) {
        //判断是否为空值
        if (StrUtil.isEmpty(mobile) || StrUtil.isEmpty(type)) {
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        //手机号校验
        if (!PhoneNumCheckUtils.isPhoneLegal(mobile)) {
            throw new NcException(ResponseEnum.INVALID_PHONE_NUMBER);
        }
        //根据不同type情况运行
        if ("2".equals(type)) {
            Boolean flag = sysCompanyUserService.checkExists(mobile);
            if (flag) {
                throw new NcException(ResponseEnum.USER_MOBILE_EXISTS);
            }
        } else {
            Boolean flag = sysCompanyUserService.checkExists(mobile);
            if (!flag) {
                throw new NcException(ResponseEnum.USER_NOT_REGISTER);
            }
        }
        //生成随机验证码
        String ran = RandomUtil.randomNumbers(6);
        //储存验证码到redis
        String key = getPrefix(type) + mobile;
        redisTemplate.boundValueOps(key).set(ran,5, TimeUnit.MINUTES);
        //发送短信
//        Boolean aBoolean = smsUtils.sendSMS(mobile, ran);
//        if (!aBoolean) {
//            throw new NcException(ResponseEnum.SEND_MESSAGE_ERROR);
//        }
        log.info(ran);

    }

    private String getPrefix(String type) {
        switch (type){
            case SMSConstant.SMS_TYPE_LOGIN:
                return SMSConstant.SMS_LOGIN_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_REGISTER:
                return SMSConstant.SMS_REGISTER_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_CHANGE_MANAGER:
                return SMSConstant.SMS_CHANGE_MANAGER_KEY_PREFIX;
            case SMSConstant.SMS_TYPE_RESET_PASSWORD:
                return SMSConstant.SMS_RESET_KEY_PREFIX;
            default:
                return null;
        }

    }

    @Override
    public Boolean checkCode(String checkcode, String mobile, String type) {
        //判断是否为空值
        if (StrUtil.isEmpty(mobile) || StrUtil.isEmpty(type)) {
            throw new NcException(ResponseEnum.INVALID_PARAM_ERROR);
        }
        //手机号校验
        if (!PhoneNumCheckUtils.isPhoneLegal(mobile)) {
            throw new NcException(ResponseEnum.INVALID_PHONE_NUMBER);
        }
        //根据不同type情况运行
        if ("2".equals(type)) {
            Boolean flag = sysCompanyUserService.checkExists(mobile);
            if (flag) {
                throw new NcException(ResponseEnum.USER_MOBILE_EXISTS);
            }
        } else {
            Boolean flag = sysCompanyUserService.checkExists(mobile);
            if (!flag) {
                throw new NcException(ResponseEnum.USER_NOT_REGISTER);
            }
        }
        //从redis拿到值
        String key = getPrefix(type) + mobile;
        String o = (String) redisTemplate.boundValueOps(key).get();
        return StrUtil.isNotEmpty(o)&&checkcode.equals(o);

    }
}
